package v1

import (
	"team-evaluate/global"
	"team-evaluate/internal/model"
	"team-evaluate/internal/service"
	"team-evaluate/pkg/app"
	"team-evaluate/pkg/convert"
	"team-evaluate/pkg/errcode"
	"team-evaluate/pkg/util"

	"github.com/gin-gonic/gin"
)

type Team struct{}

func NewTeam() Team {
	return Team{}
}

// @Summary 根据id查找团队
// @Produce  json
// @Tags 团队管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param id path int true "团队 ID"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/team/{id} [get]
func (t Team) FindById(c *gin.Context) {
	param := model.FindByIdTeamRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	team, err := svc.FindTeamById(&param)
	if err != nil {
		global.Logger.Errorf("svc.FindTeamById err: %v", err)
		response.ToErrorResponse(errcode.ErrorFindByIdTeamFail.WithDetails(err.Error()))
		return
	}
	response.ToResponseObj(team)
	return
}

// @Summary 根据name查找团队
// @Produce  json
// @Tags 团队管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param id query int true "团队 name"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/team/name [get]
func (t Team) FindByName(c *gin.Context) {
	param := model.FindByNameTeamRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	team, err := svc.FindTeamByName(&param)
	if err != nil {
		global.Logger.Errorf("svc.FindTeamByName err: %v", err)
		response.ToErrorResponse(errcode.ErrorFindByNameTeamFail.WithDetails(err.Error()))
		return
	}
	response.ToResponseObj(gin.H{
		"list": team,
	})
	return
}

// @Summary 获取多个团队
// @Produce  json
// @Tags 团队管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param created_by query int true "创建人id"
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/teams [get]
func (t Team) List(c *gin.Context) {
	param := model.ListTeamRequest{}
	param.Pager = app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	totalRows, teams, err1 := svc.GetTeamList(&param)
	if err1 != nil {
		global.Logger.Errorf("svc.CreateTeam err: %v", err)
		response.ToErrorResponse(err1)
		return
	}
	response.ToResponseList(teams, totalRows)
	return
}

// @Summary 新增团队
// @Produce  json
// @Tags 团队管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param name body string true "团队名称" maxlength(20)
// @Param created_by body int true "创建人id"
// @Param Desc body string false "描述" maxlength(100)
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/team [post]
func (t Team) Create(c *gin.Context) {
	param := model.CreateTeamRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	err = svc.CreateTeam(&param)
	if err != nil {
		global.Logger.Errorf("svc.CreateTeam err: %v", err)
		response.ToErrorResponse(errcode.ErrorCreateTeamFail.WithDetails(err.Error()))
		return
	}
	response.ToResponse(nil)
	return
}

// @Summary 更新团队
// @Produce  json
// @Tags 团队管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param id path int true "团队 ID"
// @Param name body string false "团队名称"
// @Param desc body string false "团队描述" maxlength(1000)
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/team/{id} [put]
func (t Team) Update(c *gin.Context) {
	param := model.UpdateTeamRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	err = svc.UpdateTeam(&param)
	if err != nil {
		global.Logger.Errorf("svc.UpdateTeam err: %v", err)
		response.ToErrorResponse(errcode.ErrorUpdateTeamFail.WithDetails(err.Error()))
		return
	}
	response.ToResponse(nil)
	return
}

// @Summary 删除团队
// @Produce  json
// @Tags 团队管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param id path int true "团队 ID"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/team/{id} [delete]
func (t Team) Delete(c *gin.Context) {
	param := model.DeleteTeamRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	err = svc.DeleteTeam(&param)
	if err != nil {
		global.Logger.Errorf("svc.DeleteTeam err: %v", err)
		response.ToErrorResponse(errcode.ErrorDeleteTeamFail.WithDetails(err.Error()))
		return
	}

	response.ToResponse(nil)
}
